//
//  SYHomeFTableViewCell.m
//  BSHOUYIFANG
//
//  Created by 曹雁彬 on 15/8/13.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYHomeFTableViewCell.h"
#import "Header.h"
#define kNumber 0.8
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>
@implementation SYHomeFTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
   
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        CGFloat x = 15;
        CGFloat y = 10;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        for (int i =1; i<9; i++) {
            
           ButtonView *view  = [[ButtonView alloc]initWithFrame:CGRectMake(x, y, kNumber*(SCREEN_WIDTH-20)/4, kNumber*(SCREEN_HEIGHT/5+70)/2-5)];
            view.tag = 1100+i;
            [view.button addTarget:self action:@selector(topButtonAction:) forControlEvents:UIControlEventTouchUpInside];

            [self.contentView addSubview:view];

            x +=(SCREEN_WIDTH-10)/4;
            if (i==4) {
                x = 15;
                y += (SCREEN_HEIGHT/5+30)/2;
            }

        }
    
  }return self;
}
//每个button的响应事件
- (void)topButtonAction:(UIButton *)sender
{
    if (_homeBlock!=nil) {
        //回调vc的跳转方法
        ButtonView *buttonView = (ButtonView *)[sender superview];
        NSString *str = buttonView.nameLabel.text;
        _homeBlock(str);
      

    }
    
}
-(void)setModel:(SYHomeModel *)model
{
    _model = model;
   
    for (int i = 1 ; i<9; i++) {
        
        ButtonView *view = (ButtonView *)[self.contentView viewWithTag:1100+i];
        [view.button sd_setBackgroundImageWithURL:[NSURL URLWithString:_model.data[i-1][@"img"]] forState:UIControlStateNormal];
        view.nameLabel.text = _model.data[i-1][@"name"];
    }
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@implementation ButtonView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
//        CGFloat x = frame.origin.x;
//        CGFloat y = frame.origin.y;
        //_button.backgroundColor = [UIColor orangeColor];
        CGFloat h = frame.size.height;
        CGFloat w = frame.size.width;
        _button.frame = CGRectMake(12, 0, w-24, w-24);
        _button.layer.cornerRadius = (w-24)/2;
        _button.layer.masksToBounds = YES;
        [self addSubview:_button];
        
        
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10+h-35, w, 20)];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        //_nameLabel.backgroundColor = [UIColor redColor];
        [self addSubview:_nameLabel];
        //self.backgroundColor = [UIColor cyanColor];
    }return self;
}





@end
